package com.example.contextcube;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.constraintlayout.motion.widget.MotionLayout;
import androidx.preference.PreferenceManager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.example.contextcube.base.BaseActivity;
import com.example.contextcube.util.FontSizeManager;
import com.example.contextcube.util.LocaleManager;
import com.example.contextcube.util.SharedPrefsUtil;

public class SplashActivity extends BaseActivity {
    private static final String TAG = "SplashActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 在设置主题前应用夜间模式
        applyDarkModeSettings();
        
        // 初始化语言设置
        initializeLanguageSettings();
        
        // 进入应用时清除登录状态，强制重新登录
        SharedPrefsUtil.clearLoginState(this);
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        MotionLayout motionLayout = findViewById(R.id.motion_layout);
        motionLayout.setTransitionListener(new MotionLayout.TransitionListener() {
            @Override
            public void onTransitionStarted(MotionLayout motionLayout, int startId, int endId) {}

            @Override
            public void onTransitionChange(MotionLayout motionLayout, int startId, int endId, float progress) {}

            @Override
            public void onTransitionCompleted(MotionLayout motionLayout, int currentId) {
                // 启动登录页面
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                finish();
            }

            @Override
            public void onTransitionTrigger(MotionLayout motionLayout, int triggerId, boolean positive, float progress) {}
        });
    }
    
    /**
     * 应用夜间模式设置
     */
    private void applyDarkModeSettings() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isDarkMode = prefs.getBoolean("dark_mode", false);
        
            int nightMode = isDarkMode ? 
                    AppCompatDelegate.MODE_NIGHT_YES : 
                    AppCompatDelegate.MODE_NIGHT_NO;
        
            AppCompatDelegate.setDefaultNightMode(nightMode);
    }
    
    /**
     * 初始化语言设置
     */
    private void initializeLanguageSettings() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String language = prefs.getString("language", LocaleManager.LANGUAGE_CHINESE);
        
            // 应用语言设置
        LocaleManager.getInstance().setLocale(this, language);
        
        // 应用字体大小设置
        String fontSize = prefs.getString("font_size", "medium");
        FontSizeManager.getInstance().applyFontSize(this, fontSize);
    }
} 